package servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Author:shixiaojun@itany.com
 * Date:2022/4/11 14:46
 * Version:1.0-SNAPSHOT
 * Description:
 */
// loadOnStartup属性:配置服务器启动时执行init方法
    // 其值是一个数字,值越小,优先级越高
// initParams属性:配置初始化参数
    // 其值是一个数组
    // 数组中每一个元素表示一组初始化参数
    // 每一个元素均需要通过对应的注解@WebInitParam进行标注
    // name表示key,value表示值
    // 由于当前Servlet是继承HttpServlet
    // HttpServlet属于GenericServlet的子类
    // 在GenericServlet中实现了初始化参数的方法
    // 因此,可以在子类中调用父类的方法
@WebServlet(urlPatterns = "/life3",loadOnStartup = 2,initParams = {
    @WebInitParam(name = "username",value = "alice"),
    @WebInitParam(name = "password",value = "123456789")
})
public class LifeThreeServlet extends HttpServlet {

    @Override
    public void init() throws ServletException {
        System.out.println("**************初始化方法**************");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
    }

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("业务逻辑方法");
        String username = getInitParameter("username");
        String password = getInitParameter("password");
        System.out.println("username:"+username+",password:"+password);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行POST请求");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("执行GET请求");
    }

    @Override
    public void destroy() {
        System.out.println("销毁方法");
    }
}
